package cn.senjoeson.healthsport.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import cn.senjoeson.healthsport.GreenDaoManager;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.bean.StepData;
import cn.senjoeson.healthsport.dao.StepDataDao;
import cn.senjoeson.healthsport.utils.SPUtils;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;

public class HistoryActivity extends AppCompatActivity {
    private ColumnChartView ccv_month;

    private class MonthStepInfo {
        public String Month;//表示月份
        public ArrayList<DayStepInfo> DayStepInfos;

        public MonthStepInfo(String month) {
            Month = month;
            DayStepInfos = new ArrayList<>();
        }
    }

    private class DayStepInfo {
        public Date DateInfo;
        public int Step;

        public DayStepInfo(Date dateInfo, int step) {
            DateInfo = dateInfo;
            Step = step;
        }
    }

    //随机生成小于12000的数
    private int getRandom(int max) {
        Random random = new Random();
        return random.nextInt(max);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ccv_month = findViewById(R.id.ccv_month);
        drawDayChart();


    }

    //当选中月份的柱子，绘制当月每天行走的记录
    private void drawDayChart() {
        String userid = SPUtils.getString("userid", "");
        StepDataDao stepDataDao = GreenDaoManager.getInstance().getSession().getStepDataDao();
        List<StepData> list = stepDataDao.queryBuilder()
                .where(StepDataDao.Properties.Userid.eq(0))
                .orderAsc(StepDataDao.Properties.Today).build().list();
      //  List<StepData> list = stepDataDao.queryRaw("where userid =?", userid);
        ColumnChartData columnChartData;
        Axis axisX = new Axis();
        Axis axisY = new Axis();
        List<Column> columns = new ArrayList<>();
        //SubcolumnValue 是下图中柱子中的小柱子的实现类
        List<AxisValue> axisValuess = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<SubcolumnValue> values = new ArrayList<>();
            values.add(new SubcolumnValue(Integer.parseInt(list.get(i).getStep()) + 0.5f, ChartUtils.pickColor()));
            //初始化Column
            Column column = new Column(values);
            column.setHasLabels(true);
            //添加的地方，选中时出现label
            column.setHasLabelsOnlyForSelected(true);
            columns.add(column);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);
            SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
            String today = list.get(i).getToday();
            try {
                Date date = dfs.parse(today);
                axisValuess.add(new AxisValue(i).setLabel(dfs.format(date)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //给表格添加写好数据的柱子
        columnChartData = new ColumnChartData(columns);
        axisX.setValues(axisValuess);
        axisX.setName("");
        axisY.setName("步数");
        //加入横线
        axisX.setHasLines(true);
        axisY.setHasLines(true);
        columnChartData.setAxisXBottom(axisX);//加入x轴的数据
        columnChartData.setAxisYLeft(axisY);//加入y轴的数据
        //添加的地方，选中时变粗。通常情况下点击变粗会立马缩回去，现在回变粗停住停住
        ccv_month.setValueSelectionEnabled(true);
        //给画表格的View添加要画的表格
        ccv_month.setColumnChartData(columnChartData);

    }


}
